package cn.cena.pet.controller;


import cn.cena.basic.util.AjaxResult;
import cn.cena.basic.util.PageList;
import cn.cena.pet.domain.Pet;
import cn.cena.pet.query.PetQuery;
import cn.cena.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 查询分页列表
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Pet> getPetsByQuery(@RequestBody PetQuery query) {

        return petService.queryData(query);
    }

    /**
     * 宠物上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids,Pet.STATE_OFFSALE);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("系统繁忙");
        }
    }

    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids,Pet.STATE_ONSALE);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("系统繁忙");
        }
    }

    @PostMapping("/petsListOnsale")
    public PageList<Pet> petsListOnsale(@RequestBody PetQuery query){
        query.setState(Pet.STATE_ONSALE);
        return petService.queryData(query);
    }

    @GetMapping("/petDetail/{id}")
    public Pet petDetail(@PathVariable Long id){
        return petService.loadOnsaleById(id,Pet.STATE_ONSALE);
    }

}
